import Mock from 'mockjs'

const list = []
const chargeList = []
const withdrawList = []
const loginLogList = []
const count = 100

for (let i = 0; i < count; i++) {
  chargeList.push(Mock.mock({
    id: '@increment',
    user_id: '@id',
    'money|1-3.8': 1,
    create_time: + Mock.Random.date('T'),
    update_time: + Mock.Random.date('T'),
    'status|1': [0, 1, 2, 3, 4, 5, 6], //0 等待处理，1 完成，2 失败，3 区块确认中，4 暂停上链，5 失败（取消交易），6 等待审核
    ip: '@ip'
  }))
}

for (let i = 0; i < count; i++) {
  withdrawList.push(Mock.mock({
    id: '@increment',
    user_id: '@id',
    'request_money|1-3.8': 1,
    'receive_money|1-3.8': 1,
    'fee|0.8': 1,
    create_time: + Mock.Random.date('T'),
    update_time: + Mock.Random.date('T'),
    complete_time: + Mock.Random.date('T'),
    'status|1': [0, 1, 2, 3, 4, 5, 6], //0 等待处理，1 完成，2 失败，3 区块确认中，4 暂停上链，5 失败（取消交易），6 等待审核
    ip: '@ip'
  }))
}

for (let i = 0; i < count; i++) {
  loginLogList.push(Mock.mock({
    id: '@increment',
    user_id: '@id',
    create_time: + Mock.Random.date('T'),
    login_ip: '@ip'
  }))
}

for (let i = 0; i < count; i++) {
  list.push(Mock.mock({
    id: '@increment',
    uid: '@id',
    nickname: '@ctitle(2, 4)',
    email: '@email',
    'phone|13000000000-15999999999': 1,
    'type|1': [0, 1],
    'status|1': [0, 1, 2],
    create_time: + Mock.Random.date('T'),
    charge: chargeList,
    withdraw: withdrawList,
    login_log: loginLogList
  }))
}

export default [
  {
    url: '/users/list',
    type: 'get',
    response: config => {
      const { status, type, search, page = 1, limit = 20, order } = config.query

      let mockList = list.filter(item => {
        if (type && item.type !== Number(type)) return false
        if (status && item.status !== Number(status)) return false
        if (search && item.phone !== Number(search) && item.uid !== Number(search)) return false
        return true
      })

      if (order === '-id') {
        mockList = mockList.reverse()
      }

      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        code: 0,
        msg: 'ok',
        data: {
          total: mockList.length,
          page: page,
          limit: limit,
          list: pageList
        }
      }
    }
  },

  {
    url: '/users/info',
    type: 'get',
    response: config => {
      const { id } = config.query
      for (const user of list) {
        if (user.id === -id) {
          return {
            code: 0,
            msg: 'ok',
            data: user
          }
        }
      }
      return {
        code: 50000,
        msg: 'not found'
      }
    }
  },

  {
    url: '/users/change-status',
    type: 'post',
    response: _ => {
      return {
        code: 0,
        data: 'success'
      }
    }
  }
]

